import {
  set_user_info
} from '../actions/member';

import {
  add_project,
  get_project,
  set_project_name,
  set_project_icon,
  set_project_notify
} from '../actions/project';

function project (state = {
}, action) {
  let tmp_state = state;
  switch (action.type) {
    case set_user_info:
      let projectList = action.projectList;
      projectList.forEach((item, index, array) => {
        if (item.id in tmp_state) {
          tmp_state[item.id].name = item.name;
          tmp_state[item.id].icon = item.icon;
          tmp_state[item.id].admin = item.admin;
          tmp_state[item.id].developer = item.developer;
          tmp_state[item.id].tester = item.tester;
        } else {
          tmp_state[item.id] = {
            name: item.name,
            icon: item.icon,
            admin: item.admin,
            developer: item.developer,
            tester: item.tester,
            fatDeveloper: [],
            fatAdmin: [],
            fatTester: [],
          };
        }
      })
      return Object.assign({}, state, tmp_state);
    case add_project:
      tmp_state[action.id] = {
        name: action.name
      };
      return Object.assign({}, state, tmp_state);
    case set_project_notify:
      tmp_state[action.id].chatType = action.chatType;
      tmp_state[action.id].chatHookUrl = action.chatHookUrl;
      return Object.assign({}, state, tmp_state);
    case set_project_icon:
      tmp_state[action.id].icon = action.icon;
      return Object.assign({}, state, tmp_state);
    case set_project_name:
      tmp_state[action.id].name = action.name;
      return Object.assign({}, state, tmp_state);
    case get_project:
      if (action.id in tmp_state) {
        tmp_state[action.id].name = action.name;
        tmp_state[action.id].icon = action.icon;
        tmp_state[action.id].chatType = action.chatType;
        tmp_state[action.id].chatHookUrl = action.chatHookUrl;
        tmp_state[action.id].fatAdmin = action.fatAdmin;
        tmp_state[action.id].fatDeveloper = action.fatDeveloper;
        tmp_state[action.id].fatTester = action.fatTester;
      } else {
        tmp_state[action.id] = {
          name: action.name,
          icon: action.icon,
          chatType: action.chatType,
          chatHookUrl: action.chatHookUrl,
          admin: [],
          developer: [],
          tester: [],
          fatDeveloper: action.fatTester,
          fatAdmin: action.fatAdmin,
          fatTester: action.fatTester,
        };
      }
      return Object.assign({}, state, tmp_state);
    default:
      return state;
  }
}

export default project;